import 'package:blurb/common/models/comment.dart';
import 'package:blurb/common/services/comment_service.dart';
import 'package:mobx/mobx.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter/material.dart';

// 包含生成的代码部分
part 'comment_store.g.dart';

// 使用命令生成代码：flutter packages pub run build_runner build
class CommentStore = _CommentStore with _$CommentStore;

abstract class _CommentStore with Store {
  @observable
  List<Comment> comments = [];
  
  @observable
  bool isLoading = false;

  @action
  Future<void> init() async {
    isLoading = true;
    try {
      // 初始化默认评论
      await CommentService.initDefaultComments();
      // 加载所有评论
      comments = await CommentService.getAllComments();
    } finally {
      isLoading = false;
    }
  }

  @action
  Future<List<Comment>> getShareComments(int shareId) async {
    isLoading = true;
    try {
      return await CommentService.getShareComments(shareId);
    } finally {
      isLoading = false;
    }
  }

  @action
  Future<bool> addComment(String content, int userId, int shareId) async {
    isLoading = true;
    try {
      if (content.isEmpty) {
        Fluttertoast.showToast(
          msg: "Comment cannot be empty",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          backgroundColor: Colors.red,
          textColor: Colors.white,
        );
        return false;
      }

      // 获取所有评论，确定新评论ID
      final allComments = await CommentService.getAllComments();
      final newCommentId = allComments.isEmpty ? 1 : allComments.map((comment) => comment.id).reduce((a, b) => a > b ? a : b) + 1;

      // 创建新评论
      final newComment = Comment(
        id: newCommentId,
        content: content,
        time: DateTime.now().toIso8601String(),
        userId: userId,
        shareId: shareId,
      );

      // 添加新评论
      final success = await CommentService.addComment(newComment);
      if (success) {
        // 更新本地列表
        comments = [...comments, newComment];
        Fluttertoast.showToast(
          msg: "Comment added",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          backgroundColor: Colors.green,
          textColor: Colors.white,
        );
        return true;
      } else {
        Fluttertoast.showToast(
          msg: "Failed to add comment",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          backgroundColor: Colors.red,
          textColor: Colors.white,
        );
        return false;
      }
    } finally {
      isLoading = false;
    }
  }

  @action
  Future<bool> deleteComment(int commentId) async {
    isLoading = true;
    try {
      final success = await CommentService.deleteComment(commentId);
      if (success) {
        // 更新本地列表
        comments = comments.where((comment) => comment.id != commentId).toList();
        
        Fluttertoast.showToast(
          msg: "Comment deleted",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          backgroundColor: Colors.green,
          textColor: Colors.white,
        );
        return true;
      } else {
        Fluttertoast.showToast(
          msg: "Failed to delete comment",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          backgroundColor: Colors.red,
          textColor: Colors.white,
        );
        return false;
      }
    } finally {
      isLoading = false;
    }
  }
  
  @action
  Future<void> refreshComments() async {
    isLoading = true;
    try {
      comments = await CommentService.getAllComments();
    } finally {
      isLoading = false;
    }
  }
} 